package com.netease.xone.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.netease.framework.widget.LinkedListAdapter;
import com.netease.xone.R;
import com.netease.xone.test.FakeBannerInfo;
import com.netease.xone.test.IBannerInfo;
import com.netease.xone.view.BannerView;

public class HeadLineFragment extends Fragment {
	private BannerView mBanner;
	private ListView mListView;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
			View v = inflater.inflate(R.layout.head_line_layout, null);
			mBanner = (BannerView) inflater.inflate(R.layout.banner_layout, null);
	        mListView = (ListView) v.findViewById(R.id.head_line_list);
	        
		return v;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
        mBanner.setData(FakeBannerInfo.createInfos(getActivity()));
        mBanner.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				IBannerInfo info = mBanner.getItem(position);
				Toast.makeText(getActivity(), info.getTitle(), Toast.LENGTH_SHORT).show();
			}
		});
        mListView.addHeaderView(mBanner);
        
        ListAdapter adapter = new LinkedListAdapter<String>() {
        	@Override
        	public int getCount() {
        		return 50;
        	}
        	
			@Override
			public View getView(int position, View convertView,
					ViewGroup parent) {
				TextView tv = new TextView(getActivity());
				tv.setText("text:"+position);
				return tv;
			}
        	
		};
		mListView.setAdapter(adapter);
		
		if (null != savedInstanceState) {
			int firstItem = savedInstanceState.getInt("firstitem");
			int firstItemTop = savedInstanceState.getInt("firstItemtop");
			mListView.setSelectionFromTop(firstItem, firstItemTop);
			
			int position = savedInstanceState.getInt("position");
			mBanner.setPosition(position);
		}
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		if (null !=  mListView) {
			int firstItem = mListView.getFirstVisiblePosition();
			if (null != mListView.getChildAt(0)) {
				int firstItemTop = mListView.getChildAt(0).getTop();
				outState.putInt("firstitem", firstItem);
				outState.putInt("firstItemtop", firstItemTop);
			}
		}
		
		if (null != mBanner) {
			int position = mBanner.getPosition();
			outState.putInt("position", position);
		}
		
		super.onSaveInstanceState(outState);
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		if (null != mBanner) {
			mBanner.clear();
		}
		mBanner = null;
		mListView = null;
	}
}
